import time

from django.shortcuts import render, HttpResponse, redirect
from django.http import JsonResponse
from web.forms.project import ProjectForm
from web import models
from untils.tencents.cos import create_bucket


def project_list(request):
    if request.method == 'GET':
        # 创建一个字典里面存放着已星标的项目，创建的项目，参与的项目
        project_dict = {'star': [], 'my': [], 'join': []}

        # 查询当前用户已创建的项目
        my_project_list = models.Project.objects.filter(creator=request.tracer.user)
        # 循环列表是星标的就放到star列表，不是就放到my列表
        for row in my_project_list:
            if row.star:
                project_dict['star'].append({'value': row, 'type': 'my'})
            else:
                project_dict['my'].append(row)

        # 查询当前用户已参与的项目
        join_project_list = models.ProjectUser.objects.filter(user=request.tracer.user)
        # 循环列表是星标就把我参与的项目信息放入star,不是就放入join
        for item in join_project_list:
            if item.star:
                project_dict['star'].append({'value': item.project, 'type': 'join'})
            else:
                project_dict['join'].append(item.project)

        """在star中添加字典是为了用type区分取消星标后的数据修改"""

        form = ProjectForm(request)
        return render(request, 'project_list.html', {'form': form, 'project_dict': project_dict})

    form = ProjectForm(request, data=request.POST)
    if form.is_valid():
        # 创建腾讯COS桶
        bucket = f"{request.tracer.user.mobile_phone}-{int(time.time())}-1328546605"
        region = 'ap-guangzhou'
        create_bucket(bucket=bucket, region=region)
        # 将bucket，region写入数据库
        form.instance.bucket = bucket
        form.instance.region = region
        """
        将数据提交到表
        已经有name,color,desc
        除了creator其他都有默认
        """
        # 添加creator
        form.instance.creator = request.tracer.user
        # 提交
        instance = form.save()

        # 3. 为每个项目默认创建问题类型
        issues_type_list = []
        for item in models.IssuesType.PROJECT_INIT_LIST:
            issues_type_list.append(models.IssuesType(project=instance, title=item))
            print(models.IssuesType(project=instance, title=item))
        models.IssuesType.objects.bulk_create(issues_type_list)

        return JsonResponse({'status': True})

    return JsonResponse({'status': False, 'errors': form.errors})


def project_star(request, project_type, project_id):
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=True)
        return redirect('project_list')
    if project_type == 'join':
        models.ProjectUser.objects.filter(id=project_id, user=request.tracer.user).update(star=True)
        return redirect('project_list')
    return HttpResponse('请求错误')


def project_unstar(request, project_type, project_id):
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=False)
        return redirect('project_list')
    if project_type == 'join':
        models.ProjectUser.objects.filter(id=project_id, user=request.tracer.user).update(star=False)
        return redirect('project_list')
    return HttpResponse('请求错误')
